package com.isay.frameworklib.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 时间格式化工具类
 *
 * @author qingwu
 */
public class DateUtils {

    public enum DateFormater {

        NORMAL("yyyy-MM-dd HH:mm"), DD("yyyy-MM-dd"), SS("yyyy-MM-dd HH:mm:ss");

        private String value;

        private DateFormater(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }


    /**
     * 判断是否是同一天
     *
     * @param oldTime
     * @param currTime
     * @return
     */
    public static boolean isToday(long oldTime, long currTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateFormater.DD.getValue());
        String oldD = sdf.format(oldTime);
        String currD = sdf.format(currTime);
        return oldD.equals(currD);
    }

    public static String getCurrentDateFormat(String pattern) {
        final DateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date());
    }

    public static String getDateFormat(String pattern, String time) {
        final DateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return format.format(date);
    }

    public static String getDateToStrFormat(String dateType, Date time) {
        final DateFormat format = new SimpleDateFormat(dateType);
        return format.format(time);
    }

    /**
     * 时间戳转时间
     *
     * @param time
     * @return
     */
    public static String getLongDateToString(long time) {
        return getLongDateToString(DateFormater.DD.getValue(), time);
    }

    /**
     * 时间戳转时间
     *
     * @param time
     * @return
     */
    public static String getLongDateToString(String dateType, long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat(dateType);
        return sf.format(d);
    }

    /**
     * 时间字符串转时间戳 必须是 SS形式
     *
     * @param time
     * @return
     */
    public static long getStringDateToLong(String dateType, String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateType);
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
        long timeStemp = date.getTime();
        return timeStemp;
    }

    public static long getStringDateToLong(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateFormater.DD.getValue());
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(time);
            SimpleDateFormat formatSS = new SimpleDateFormat(DateFormater.SS.getValue());
            String timess = formatSS.format(date);
            return getStringDateToLong(DateFormater.SS.getValue(), timess);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }


    }

    /**
     * 时间比较
     * >0:oldTime > newTime <0:newTime > oldTime =0:newTime = oldTime
     *
     * @param oldTime
     * @param newTime
     * @return
     */
    public static long timeCompare(String oldTime, String newTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateFormater.SS.getValue());
        Date d1 = null, d2 = null;
        long ret = 0;
        try {
            d1 = sdf.parse(oldTime);
            d2 = sdf.parse(newTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (d1 != null && d2 != null) {
            ret = d1.getTime() - d2.getTime();
        }
        return ret;
    }

    /** */
    /**
     * 根据一个日期，返回是星期几的字符串
     *
     * @param sdate
     * @return
     */
    public static String getWeek(String sdate) {
        // 再转换为时间
        Date date = VeDate.strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // int hour=c.get(Calendar.DAY_OF_WEEK);
        // hour中存的就是星期几了，其范围 1~7
        // 1=星期日 7=星期六，其他类推
        return new SimpleDateFormat("EE").format(c.getTime());
    }

    static class VeDate {
        public static Date strToDate(String strDate) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDate, pos);
            return strtodate;
        }
    }


}
